import { defineStore } from "pinia";
import { ref } from "vue";
import { getAppSettingsApi } from "@/api/common";
import { allCategoryListApi } from '@/api/goods';
import { useTask } from "@/utils/message";

export const useSettingStore = defineStore("setting", () => {
  const setting = ref<AnyObject>({}); // 用户信息
  const allCategoryList = ref<any[]>([]); // 商品分类列表

    const getSetting = async () => {
        const res: AnyObject = await getAppSettingsApi({});
        if (res.code == 200) {
            const data = res.data;
            if (Object.keys(data).length > 0) {
                for (const item of data) {
                    setting.value[item.key] = item.value;
              }
            } else {
              console.log('没有系统配置')
            }
        }
    };

  const getAllCategoryList = async () => {
    const res = await allCategoryListApi({});
    if (res.code == 200) {
      allCategoryList.value = res.data;
    } else {
      useTask(res.msg);
    }
  }

    return {
        setting,
        allCategoryList,
        getSetting,
        getAllCategoryList
    };
});
